package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.zzw.BO.AddTPeopleEvent;
import com.ruoyi.common.core.domain.zzw.BO.AddTPeopleEventBO;
import com.ruoyi.common.core.domain.zzw.TPeopleManage;
import com.ruoyi.common.core.domain.zzw.VO.*;
import com.ruoyi.system.mapper.TRescueStrengthMapper;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TPeopleEventMapper;
import com.ruoyi.system.domain.TPeopleEvent;
import com.ruoyi.system.service.ITPeopleEventService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 突发事件人员关联表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-01
 */
@Service
public class TPeopleEventServiceImpl implements ITPeopleEventService 
{
    @Autowired
    private TPeopleEventMapper tPeopleEventMapper;
    @Autowired
    private TRescueStrengthMapper tRescueStrengthMapper;
    /**
     * 查询突发事件人员关联表
     * 
     * @param cPeopleDuty 突发事件人员关联表主键
     * @return 突发事件人员关联表
     */
    @Override
    public TPeopleEvent selectTPeopleEventByCPeopleDuty(Long cPeopleDuty)
    {
        return tPeopleEventMapper.selectTPeopleEventByCPeopleDuty(cPeopleDuty);
    }

    /**
     * 查询突发事件人员关联表列表
     * 
     * @param tPeopleEvent 突发事件人员关联表
     * @return 突发事件人员关联表
     */
    @Override
    public List<TPeopleEvent> selectTPeopleEventList(TPeopleEvent tPeopleEvent)
    {
        return tPeopleEventMapper.selectTPeopleEventList(tPeopleEvent);
    }

    /**
     * 新增突发事件人员关联表
     * 
     * @param tPeopleEvent 突发事件人员关联表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTPeopleEvent(AddTPeopleEventBO addTPeopleEventBO)
    {
        //先删除当前事件下的所有数据
        tPeopleEventMapper.deleteTPeopleEventByCPeopleDuty(addTPeopleEventBO.getCEventId());
        List<AddTPeopleEvent> deptAndPeople = addTPeopleEventBO.getDeptAndPeople();
        int count=0;
        for (AddTPeopleEvent deptAndPerson : deptAndPeople) {
            TPeopleEvent tPeopleEvent =new TPeopleEvent();
            BeanUtils.copyProperties(deptAndPerson,tPeopleEvent);
            tPeopleEvent.setCEventId(addTPeopleEventBO.getCEventId());
            tPeopleEventMapper.insertTPeopleEvent(tPeopleEvent);
            count++;
        }
        return count;
    }

    /**
     * 修改突发事件人员关联表
     * 
     * @param tPeopleEvent 突发事件人员关联表
     * @return 结果
     */
    @Override
    public int updateTPeopleEvent(TPeopleEvent tPeopleEvent)
    {
        return tPeopleEventMapper.updateTPeopleEvent(tPeopleEvent);
    }

    /**
     * 批量删除突发事件人员关联表
     * 
     * @param cPeopleDutys 需要删除的突发事件人员关联表主键
     * @return 结果
     */
    @Override
    public int deleteTPeopleEventByCPeopleDutys(Long[] cPeopleDutys)
    {
        return tPeopleEventMapper.deleteTPeopleEventByCPeopleDutys(cPeopleDutys);
    }

    /**
     * 删除突发事件人员关联表信息
     * 
     * @param cPeopleDuty 突发事件人员关联表主键
     * @return 结果
     */
    @Override
    public int deleteTPeopleEventByCPeopleDuty(Long cPeopleDuty)
    {
        return tPeopleEventMapper.deleteTPeopleEventByCPeopleDuty(cPeopleDuty);
    }

    @Override
    public List<TPeopleManageVO> deptAndPeoByEvenId(Long cEventId) {
        List<TPeopleManageVO> tPeopleManageVOS = tPeopleEventMapper.deptAndPeoByEvenId(cEventId);
        List<TPeopleManageVO> collect = tPeopleManageVOS.stream()
                .sorted((p1, p2) -> {
                    if ("指挥长".equals(p1.getCDepartmentType())) {
                        return -1;
                    } else if ("指挥长".equals(p2.getCDepartmentType())) {
                        return 1;
                    } else {
                        return 0;
                    }
                })
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public AjaxResult echoByEvenId(Long cEventId) {
        List<EchoByEvenId> echo= tPeopleEventMapper.echoByEvenId(cEventId);
        List<TDepartmentManageVO> vos=new ArrayList<>();
        for (EchoByEvenId echoByEvenId : echo) {
            TDepartmentManageVO vo=new TDepartmentManageVO();
            BeanUtils.copyProperties(echoByEvenId,vo);
            //添加指挥长信息
            TPeopleManage leader=new TPeopleManage();
            leader.setCPeopleId(echoByEvenId.getLeaderCPeopleId());
            leader.setCPeopleName(echoByEvenId.getLeaderCPeopleName());
            leader.setCDepartmentId(echoByEvenId.getLeaderCDepartmentId());
            leader.setCPosition(echoByEvenId.getLeaderCPosition());
            vo.setLeader(leader);

            //添加值班长信息
            TPeopleManage duty=new TPeopleManage();
            duty.setCPeopleId(echoByEvenId.getDutyCPeopleId());
            duty.setCPeopleName(echoByEvenId.getDutyCPeopleName());
            duty.setCDepartmentId(echoByEvenId.getDutyCDepartmentId());
            duty.setCPosition(echoByEvenId.getDutyCPosition());

            vo.setDuty(duty);
            vos.add(vo);
        }
        //提取指挥长为第一个
        LinkedMap<String,TDepartmentManageVO> maps=new LinkedMap<>();
        for (TDepartmentManageVO vo : vos) {
            maps.put(vo.getCType(),vo);
        }
        return AjaxResult.success(maps);
    }


}
